﻿using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;

namespace tjb.Utility.Gather
{
    /// <summary>
    /// 
    /// </summary>
    public class GatherMethods
    {
        /// <summary>
        /// 根据各个操作类型处理字符串
        /// </summary>
        /// <param name="str">要处理的字符串</param>
        /// <param name="startstr">开始字符串</param>
        /// <param name="endstr">结束字符串</param>
        /// <param name="Newstr">替换/前缀/后缀的字符串</param>
        /// <param name="operate">操作类型</param>
        /// <param name="inHead">包含头1，不包含头0</param>
        /// <param name="inTail">包含尾1，不包含尾0</param>
        /// <returns>返回处理过后的字符串</returns>
        public string OperateStr(string str, string startstr, string endstr, string Newstr, int operate, int inHead, int inTail)
        {
            int intLength = str.Length;
            int startIndex = str.IndexOf(startstr);
            int endIndex = str.IndexOf(endstr);
            switch (operate)
            {
                case 0:
                    break;
                case 1://截取
                    if (endIndex <= startIndex || startIndex == -1)
                        str = "";
                    if (inHead == 1 && inTail == 1)//表示包含头尾,保证能找到尾
                    {
                        str = str.Substring(startIndex, endIndex - startIndex + endstr.Length);

                    }

                    if (inHead == 1 && inTail == 0)//表示包含头，不包含尾
                    {
                        str = str.Substring(startIndex, endIndex - startIndex);
                    }

                    if (inHead == 0 && inTail == 1)//表示不包含头，包含尾
                    {
                        str = str.Substring(startIndex + startstr.Length, endIndex - startIndex - startstr.Length + endstr.Length);
                    }

                    if (inHead == 0 && inTail == 0)//表示不包含头，也不包含尾
                    {
                        str = str.Substring(startIndex + startstr.Length, endIndex - startIndex - startstr.Length);
                    }
                    break;
                case 2://删除段
                    if (endIndex <= startIndex || startIndex == -1)
                    {
                    }

                    if (inHead == 1 && inTail == 1)//表示包含头尾
                    {
                        str = str.Replace(str.Substring(startIndex, endIndex - startIndex + endstr.Length), "");
                    }

                    if (inHead == 1 && inTail == 0)//表示包含头，不包含尾
                    {
                        str = str.Replace(str.Substring(startIndex, endIndex - startIndex), "");
                    }

                    if (inHead == 0 && inTail == 1)//表示不包含头，包含尾
                    {
                        str = str.Replace(str.Substring(startIndex + startstr.Length, endIndex - startIndex - startstr.Length + endstr.Length), "");
                    }

                    if (inHead == 0 && inTail == 0)//表示不包含头，也不包含尾
                    {
                        str = str.Replace(str.Substring(startIndex + startstr.Length, endIndex - startIndex - startstr.Length), "");
                    }
                    break;
                case 3://删除字符串
                    str = str.Replace(startstr, "");
                    break;
                //break;
                case 4://字符串前缀
                    str = str.Replace(startstr, Newstr + startstr);
                    break;
                //break;
                case 5://段落前缀
                    if (endIndex <= startIndex || startIndex == -1)
                    {
                    }
                    if (inHead == 1)
                        str = str.Replace(str.Substring(startIndex, endIndex - startIndex), Newstr + str.Substring(startIndex, endIndex - startIndex));

                    if (inHead == 0)
                        str = str.Replace(str.Substring(startIndex + startstr.Length, endIndex - startIndex - startstr.Length), Newstr + str.Substring(startIndex + startstr.Length, endIndex - startIndex - startstr.Length));
                    break;
                case 6://字符串后缀
                    str = str.Replace(startstr, startstr + Newstr);
                    break;
                // break;
                case 7://段落后缀
                    if (endIndex <= startIndex || startIndex == -1)
                    {
                    }
                    if (inTail == 1)
                        str = str.Replace(str.Substring(startIndex, endIndex - startIndex + endstr.Length), str.Substring(startIndex, endIndex - startIndex + endstr.Length) + Newstr);

                    if (inTail == 0)
                        str = str.Replace(str.Substring(startIndex, endIndex - startIndex), Newstr + str.Substring(startIndex, endIndex - startIndex) + Newstr);
                    break;
                case 8://替换字符串
                    str = str.Replace(startstr, Newstr);
                    break;
                //break;
                case 9://替换段落
                    if (inHead == 1 && inTail == 1)//表示包含头尾
                    {
                        str = str.Replace(str.Substring(startIndex, endIndex - startIndex + endstr.Length), Newstr);
                    }

                    if (inHead == 1 && inTail == 0)//表示包含头，不包含尾
                    {
                        str = str.Replace(str.Substring(startIndex, endIndex - startIndex), Newstr);
                    }

                    if (inHead == 0 && inTail == 1)//表示不包含头，包含尾
                    {
                        str = str.Replace(str.Substring(startIndex + startstr.Length, endIndex - startIndex - startstr.Length + endstr.Length), Newstr);
                    }

                    if (inHead == 0 && inTail == 0)//表示不包含头，也不包含尾
                    {
                        str = str.Replace(str.Substring(startIndex + startstr.Length, endIndex - startIndex - startstr.Length), Newstr);
                    }

                    break;
                default:
                    throw new Exception("操作符超出界限，operate 不在[0-9]范围内。");
            }

            return str;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="rules"></param>
        /// <returns></returns>
        public string OperateStr(string str, List<Rule> rules)
        {
            List<Rule>.Enumerator enu = rules.GetEnumerator();

            while (enu.MoveNext())
            {
                str = OperateStr(str, enu.Current);
            }
            return str;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="rule"></param>
        /// <returns></returns>
        public string OperateStr(string str, Rule rule)
        {
            int intLength = str.Length;
            int startIndex = str.IndexOf(rule.Primality);
            int endIndex = str.IndexOf(rule.End);
            switch (rule.Operate)
            {
                case OperateType.None:
                    break;
                case OperateType.Cut://截取
                    if (endIndex <= startIndex || startIndex == -1)
                        str = "";

                    if (rule.IncludedHead && rule.IncludedTail)//表示包含头尾,保证能找到尾
                    {
                        str = str.Substring(startIndex, endIndex - startIndex + rule.End.Length);
                    }

                    if (rule.IncludedHead && (!rule.IncludedTail))//表示包含头，不包含尾
                    {
                        str = str.Substring(startIndex, endIndex - startIndex);
                    }

                    if ((!rule.IncludedHead) && rule.IncludedTail)//表示不包含头，包含尾
                    {
                        str = str.Substring(startIndex + rule.Primality.Length, endIndex - startIndex - rule.Primality.Length + rule.End.Length);
                    }

                    if ((!rule.IncludedHead) && (!rule.IncludedTail))//表示不包含头，也不包含尾
                    {
                        str = str.Substring(startIndex + rule.Primality.Length, endIndex - startIndex - rule.Primality.Length);
                    }
                    break;
                case OperateType.DelPara://删除段
                    if (endIndex <= startIndex || startIndex == -1)
                    {
                    }

                    if (rule.IncludedHead && rule.IncludedTail)//表示包含头尾
                    {
                        str = str.Replace(str.Substring(startIndex, endIndex - startIndex + rule.End.Length), "");
                    }

                    if (rule.IncludedHead && (!rule.IncludedTail))//表示包含头，不包含尾
                    {
                        str = str.Replace(str.Substring(startIndex, endIndex - startIndex), "");
                    }

                    if ((!rule.IncludedHead) && rule.IncludedTail)//表示不包含头，包含尾
                    {
                        str = str.Replace(str.Substring(startIndex + rule.Primality.Length, endIndex - startIndex - rule.Primality.Length + rule.End.Length), "");
                    }

                    if ((!rule.IncludedHead) && (!rule.IncludedTail))//表示不包含头，也不包含尾
                    {
                        str = str.Replace(str.Substring(startIndex + rule.Primality.Length, endIndex - startIndex - rule.Primality.Length), "");
                    }
                    break;
                case OperateType.DelString://删除字符串
                    str = str.Replace(rule.Primality, "");
                    break;
                //break;
                case OperateType.StringPrefix://字符串前缀
                    str = str.Replace(rule.Primality, rule.NewStr + rule.Primality);
                    break;
                //break;
                case OperateType.ParaPrefix://段落前缀
                    if (endIndex <= startIndex || startIndex == -1)
                    {
                    }
                    if (rule.IncludedHead)
                        str = str.Replace(str.Substring(startIndex, endIndex - startIndex), rule.NewStr + str.Substring(startIndex, endIndex - startIndex));

                    if ((!rule.IncludedHead))
                        str = str.Replace(str.Substring(startIndex + rule.Primality.Length, endIndex - startIndex - rule.Primality.Length), rule.NewStr + str.Substring(startIndex + rule.Primality.Length, endIndex - startIndex - rule.Primality.Length));
                    break;
                case OperateType.StringSuffix://字符串后缀
                    str = str.Replace(rule.Primality, rule.Primality + rule.NewStr);
                    break;
                // break;
                case OperateType.ParaSuffix://段落后缀
                    if (endIndex <= startIndex || startIndex == -1)
                    {
                    }
                    if (rule.IncludedTail)
                        str = str.Replace(str.Substring(startIndex, endIndex - startIndex + rule.End.Length), str.Substring(startIndex, endIndex - startIndex + rule.End.Length) + rule.NewStr);

                    if ((!rule.IncludedTail))
                        str = str.Replace(str.Substring(startIndex, endIndex - startIndex), rule.NewStr + str.Substring(startIndex, endIndex - startIndex) + rule.NewStr);
                    break;
                case OperateType.ReplaceString://替换字符串
                    str = str.Replace(rule.Primality, rule.NewStr);
                    break;
                //break;
                case OperateType.ReplacePara://替换段落
                    if (rule.IncludedHead && rule.IncludedTail)//表示包含头尾
                    {
                        str = str.Replace(str.Substring(startIndex, endIndex - startIndex + rule.End.Length), rule.NewStr);
                    }

                    if (rule.IncludedHead && (!rule.IncludedTail))//表示包含头，不包含尾
                    {
                        str = str.Replace(str.Substring(startIndex, endIndex - startIndex), rule.NewStr);
                    }

                    if ((!rule.IncludedHead) && rule.IncludedTail)//表示不包含头，包含尾
                    {
                        str = str.Replace(str.Substring(startIndex + rule.Primality.Length, endIndex - startIndex - rule.Primality.Length + rule.End.Length), rule.NewStr);
                    }

                    if ((!rule.IncludedHead) && (!rule.IncludedTail))//表示不包含头，也不包含尾
                    {
                        str = str.Replace(str.Substring(startIndex + rule.Primality.Length, endIndex - startIndex - rule.Primality.Length), rule.NewStr);
                    }

                    break;
                case OperateType.ReplaceRegex:

                    Regex reg = new Regex(rule.RegexPattern, RegexOptions.Multiline | RegexOptions.Compiled);


                    MatchCollection mc = reg.Matches(str);



                    Match m = mc[0];


                    break;
                default:
                    throw new Exception("操作符超出界限，operate 不在[0-9]范围内。");
            }

            return str;
        }
    }
}
